package com.huike.web.controller.business;

import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.TbBusinessTrackRecordVO;
import com.huike.business.service.ITbBusinessService;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business/record")
public class TbBusinessTrackRecordController extends BaseController {
    @Autowired(required = false)
    private ITbBusinessTrackRecordService iTbBusinessTrackRecordService;
    @Autowired(required = false)
   private  ISysDictDataService iSysDictDataService;
    @Autowired(required = false)
    private ITbBusinessService iTbBusinessService;
    @GetMapping("/list")
    public AjaxResult list(@RequestParam("businessId")Long id){
        List<TbBusinessTrackRecord> list = iTbBusinessTrackRecordService.getBusinessTrackRecord(id);
        for (TbBusinessTrackRecord tbBusinessTrackRecord : list) {
            String[] items = tbBusinessTrackRecord.getKeyItems().split(",");
            for (String item : items) {
                String key = iSysDictDataService.selectDictLabel("communication_point", item);
                tbBusinessTrackRecord.getKeys().add(key);
            }
        }
        return AjaxResult.success(list);
    }

    @PostMapping
    public AjaxResult insertBusinessTrackRecord(@RequestBody TbBusinessTrackRecordVO tbBusinessTrackRecordVO){
        //建立一个TbBusinessTrackRecord
        TbBusinessTrackRecord businessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(tbBusinessTrackRecordVO,businessTrackRecord);
        businessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        businessTrackRecord.setCreateTime(DateUtils.getNowDate());
        iTbBusinessTrackRecordService.insertBusinessTrackRecord(businessTrackRecord);
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(tbBusinessTrackRecordVO,tbBusiness);
        tbBusiness.setId(tbBusinessTrackRecordVO.getBusinessId());
        tbBusiness.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        iTbBusinessService.insertTbBusiness(tbBusiness);
        return AjaxResult.success("添加成功");
    }
}
